package com.test.selenium.util;

import java.io.IOException;

import org.apache.commons.lang3.RandomStringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.test.selenium.AirSmokeTest;
import com.test.selenium.data.OutputSheet;
import com.test.selenium.data.Results;
import com.test.selenium.data.UTiLogin;

public class SmokeTestContainer {
	private boolean debugMode = false;
	
	private WebDriver driver;
	private WebDriverWait wait;
	private OutputSheet resultSheet;
	
	private String containerNum;
	private String containerStatus;
	
	public SmokeTestContainer(WebDriver driver, OutputSheet resultSheet) {
		this.driver = driver;
		this.resultSheet = resultSheet;
		wait = new WebDriverWait(driver, 60);
	}
	
	
	// To-do: Add container status validation
	public void createContainer() {
		String isPass = "PASS";
		double testTimeTakenSec = 0;
		
		try {
			String containerUrl = UTiLogin.url + "#!container";
			driver.get(containerUrl);
	        Thread.sleep(3000);
	        wait.until(ExpectedConditions.presenceOfElementLocated(By.id("containerNumber"))).sendKeys("RSA"+RandomStringUtils.randomNumeric(4)+"RA");
	        WebElement element15 = driver.findElement(By.id("containerNumber"));
	        element15.sendKeys(Keys.TAB);
	        Thread.sleep(2000);
	        
	        driver.findElement(By.xpath("/html/body/div[4]/div/div/div/div[2]/div[3]/div/div[4]/ul/li/label/a")).click();
	        new Select (driver.findElement(By.id("mode"))).selectByVisibleText("AIR");
	        driver.findElement(By.id("type")).sendKeys("M1H");
	        driver.findElement(By.id("origin")).sendKeys("LAX");
	        driver.findElement(By.id("destination")).sendKeys("JFK");
	        wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div/div[4]/button[3]"))).click();
	        Thread.sleep(4000);
	        wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div[15]/div")));
	        wait.until(ExpectedConditions.presenceOfElementLocated(By.id("reloadConfirm"))).click();
	        Thread.sleep(7000);
	        
	        wait.until(ExpectedConditions.presenceOfElementLocated(By.className("mainContentBox"))).click();
	        wait.until(ExpectedConditions.presenceOfElementLocated(By.className("header"))).click();
	        driver.findElement(By.id("editContainers")).click();
	      //Print Container Number
	        containerNum = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("containerNumber"))).getAttribute("Value");
	        
	        if (debugMode) {
	        	System.out.println("ContainerNumber = "+ containerNum);
	        }
		}
		catch (Exception e) {
        	e.printStackTrace();
        	if (e instanceof InterruptedException) {
        		// Currently ignore exception thrown by "Thread.sleep"
        	}
        	else {
        		// Other exceptions such as certain element not found considered as fail.
        		isPass = "FAIL";
        	}
        }
        finally {
        	String testValue= "Container number = " + containerNum;
        	try {
        		//Print EXCEL report	
        		resultSheet.writeTestStepResult(++AirSmokeTest.TESTSTEP, "Container", "Create container", isPass, 0, testValue);
        	}
        	catch (IOException e) {
				e.printStackTrace();
			}
        	//Print HTML report	
        	AirSmokeTest.RESULT_ARRAY.add(new Results("Container", "Create Container", isPass, testValue, "N/A"));
        }
	}
	
	public void updateContainer() {
		String isPass = "PASS";
		double testTimeTakenSec = 0;
		
		try {
			// Update Container
	        Thread.sleep(2000);
	        //driver.findElement(By.className("rowMain"));
	        driver.findElement(By.id("shipperOwned")).click();
	        wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div/div[4]/button[3]"))).click();
	        Thread.sleep(4000);
	        //Enable the Container status Grid
	        wait.until(ExpectedConditions.presenceOfElementLocated(By.className("mainContentBox"))).click();
	        wait.until(ExpectedConditions.presenceOfElementLocated(By.className("header"))).click();
	        driver.findElement(By.id("editContainers")).click();
	        wait.until(ExpectedConditions.presenceOfElementLocated(By.id("containerEditFooter"))).click();
	        wait.until(ExpectedConditions.presenceOfElementLocated(By.id("containerEditCancel"))).click();
	        Thread.sleep(4000);
	        wait.until(ExpectedConditions.presenceOfElementLocated(By.className("mainContentBox"))).click();
	        wait.until(ExpectedConditions.presenceOfElementLocated(By.className("header"))).click();
	        wait.until(ExpectedConditions.presenceOfElementLocated(By.className("rowMain"))).click();
		}
		catch (Exception e) {
        	e.printStackTrace();
        	if (e instanceof InterruptedException) {
        		// Currently ignore exception thrown by "Thread.sleep"
        	}
        	else {
        		// Other exceptions such as certain element not found considered as fail.
        		isPass = "FAIL";
        	}
        }
        finally {
        	String testValue= "Container number = " + containerNum;
        	try {
        		//Print EXCEL report
        		resultSheet.writeTestStepResult(++AirSmokeTest.TESTSTEP, "Container", "Update container", isPass, 0);
        	}
        	catch (IOException e) {
				e.printStackTrace();
			}
       	//Print HTML report
		    AirSmokeTest.RESULT_ARRAY.add(new Results("Container", "Update Container", isPass, testValue, "N/A"));
        }
	}
	
	public void assignShipment(String shipmentNumber) {
		String isPass = "PASS";
		String testTimeTakenSec="N/A";
		
		try {
			wait.until(ExpectedConditions.presenceOfElementLocated(By.id("containerFooter"))).click();
	        driver.findElement(By.id("ezShipTabA")).click();
	        wait.until(ExpectedConditions.presenceOfElementLocated(By.id("shipment_number"))).sendKeys(shipmentNumber);
	        driver.findElement(By.id("assignShipment")).click();
	        Thread.sleep(8000);
		}
		catch (Exception e) {
        	e.printStackTrace();
        	if (e instanceof InterruptedException) {
        		// Currently ignore exception thrown by "Thread.sleep"
        	}
        	else {
        		// Other exceptions such as certain element not found considered as fail.
        		isPass = "FAIL";
        	}
        }
        finally {
        	try {
        		//Print EXCEL report
        		resultSheet.writeTestStepResult(++AirSmokeTest.TESTSTEP, "Freight", "Assign shipment to container", isPass, 0);
        	}
        	catch (IOException e) {
				e.printStackTrace();
			}
        	//Print HTML report
		    AirSmokeTest.RESULT_ARRAY.add(new Results("Freight", "AssignShipmentToContainer", isPass, "N/A", testTimeTakenSec));
        }
	}
	
	public void assignToMovment(String movementNumber) {
		String isPass = "PASS";
		String testTimeTakenSec = "0";
		
		try {
			driver.findElement(By.id("ezMvmtTabA")).click();
	        wait.until(ExpectedConditions.presenceOfElementLocated(By.id("consolidation_number"))).sendKeys(movementNumber);
	        driver.findElement(By.id("assignMovement")).click();
	        Thread.sleep(8000);
		}
		catch (Exception e) {
        	e.printStackTrace();
        	if (e instanceof InterruptedException) {
        		// Currently ignore exception thrown by "Thread.sleep"
        	}
        	else {
        		// Other exceptions such as certain element not found considered as fail.
        		isPass = "FAIL";
        	}
        }
        finally {
        	try {
        		//Print EXCEL report
        		resultSheet.writeTestStepResult(++AirSmokeTest.TESTSTEP, "Freight", "Assign container to movement", isPass, 0);
        	}
        	catch (IOException e) {
				e.printStackTrace();
			}
        	//Print EXCEL report
		    AirSmokeTest.RESULT_ARRAY.add(new Results("Freight", "AssignContainerToMovement", isPass, "N/A", testTimeTakenSec));
        }
	}
}
